/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebServiceGamingClient;

import WebServiceModel.GamingStateModel;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

/**
 *
 * @author Peach
 */
public class GamingClient {
    public GamingClient() {
	try {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = "gamingService";
	methodGet = "getMessage";
	methodSend = "sendMessage";
        methodGameOver="gameOver";
    }

    public GamingClient(String URI, String methodGetString,
        String methodSendString,String methodGameOverString) {
	try {
            url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
	} catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	uri = URI;
	methodGet = methodGetString;
        methodSend = methodSendString;
        methodGameOver=methodGameOverString;
    }

    public GamingStateModel getMessage(int roomId,int deskId) {
	Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodGet);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
        params.addElement(new Parameter("roomId",int.class,roomId,null));
	params.addElement(new Parameter("deskId", int.class, deskId, null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
                System.out.println(result.getValue());
                return changeToModel((ArrayList<Integer>)result.getValue());
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
	return null;
    }

    public void sendMessage(GamingStateModel state) {
	Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodSend);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
	params.addElement(new Parameter("state", 
                ArrayList.class, changeToArray(state),null));
        call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
                Fault fault = resp.getFault();
                System.out.println("The call failed:");
                System.out.println("Fault code =" + fault.getFaultCode());
                System.out.println("Fault String =" + fault.getFaultString());
            } else {
            	Parameter result = resp.getReturnValue();
		//System.out.println(result.getValue());
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private ArrayList<Integer> changeToArray(GamingStateModel state){
        ArrayList<Integer> stateArray=new ArrayList<Integer>();
        stateArray.add(state.getRoomID());
        stateArray.add(state.getDeskID());
        stateArray.add(state.getState());
        stateArray.add(state.getPos().x);
        stateArray.add(state.getPos().y);
        stateArray.add(state.getTime());
        return stateArray;
    }
    
    private GamingStateModel changeToModel(ArrayList<Integer> state){
        GamingStateModel stateModel= new GamingStateModel();
        stateModel.setDeskID(state.get(0),state.get(1));
        stateModel.setState(state.get(2));
        stateModel.setPos(state.get(3),state.get(4));
        stateModel.setTime(state.get(5));
        return stateModel;
    }
    
    public void gameOver(int roomId,int deskId){
        Call call = new Call();
	call.setTargetObjectURI(uri);
	call.setMethodName(methodGameOver);
	call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
	Vector params = new Vector();
        params.addElement(new Parameter("roomId",int.class,roomId,null));
	params.addElement(new Parameter("deskId", int.class, deskId, null));
	call.setParams(params);
	try {
            Response resp = call.invoke(url, "");
            if (resp.generatedFault()) {
		Fault fault = resp.getFault();
		System.out.println("The call failed:");
		System.out.println("Fault code =" + fault.getFaultCode());
		System.out.println("Fault String =" + fault.getFaultString());
            } else {
		Parameter result = resp.getReturnValue();
            }
	} catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
	}
    }
    
    /*
    public static void main(String args[]) throws Exception {
	GamingClient test=new GamingClient();
        for(int i=0;i<10;i++){
            System.out.println(test.getMessage("30492"));
            GamingStateModel state=new GamingStateModel();
            state.setDeskID("30492");
            state.setState("fuck");
            state.setPos(i, i+1);
            test.sendMessage(state);
	}
    }
    */

    private URL url;
    private String uri;
    private String methodGet;
    private String methodSend;
    private String methodGameOver;
}
